package com.petalsgroup.petals.dao.admin.impl;

import com.petalsgroup.petals.dao.admin.AdminDao;
import com.petalsgroup.petals.pojo.AdministratorDO;
import com.petalsgroup.petals.pojo.AnimationSimpleDO;
import com.petalsgroup.petals.pojo.CommentsDO;
import com.petalsgroup.petals.pojo.UserDO;
import com.petalsgroup.petals.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class AdminDaoImpl implements AdminDao{
    private JdbcTemplate template=new JdbcTemplate(JDBCUtils.getDataSource());

    /**
     * 从数据库根据账号密码获取管理员
     * @param username
     * @param password
     * @author wgd
     * @return
     */
    @Override
    public AdministratorDO findAdminByUsernameAndPassword(String username, String password) {
        String sql="select * from administrator where username = ? and password = ?";
        List<AdministratorDO> list = template.query(sql,new BeanPropertyRowMapper<>(AdministratorDO.class),username,password);
        return list.size()==0 ? null : list.get(0);
    }

    /**
     * 从数据库获取一个用户信息的list
     * @author wgd
     * @return
     */
    @Override
    public List<UserDO> selectAllUser() {
        //sql编写:
        String sql = "select * from user";
        List<UserDO> list = template.query(sql,
                new BeanPropertyRowMapper<>(UserDO.class));
        return list;
    }

    /**
     * 通过uid禁言用户
     * @param uid
     * @author wgd
     */
    @Override
    public void bannedUser(String uid) {
        String sql = "update user set banned = ? where uid = ?";
        template.update(sql,"Y",uid);

    }

    /**
     * 通过uid解禁用户
     * @param uid
     * @author wgd
     */
    @Override
    public void resolveUser(String uid) {
        String sql="update user set banned = ? where uid = ?";
        template.update(sql,"N",uid);
    }

    /**
     * 通过uid对用户进行封号操作
     * @param uid
     * @author wgd
     */
    @Override
    public void deleteUser(String uid) {
        //String sql="delete from user where uid = ?";
        String sql="update user set code = ?,active_state = ? where uid = ?";
        template.update(sql,"thisUserLogoutNowLogout%$#","O",uid);
    }

    /**
     * 根据邮箱/用户名/姓名中是否包含该信息来查询用户
     * @author wgd
     * @return
     */

    @Override
    public List<UserDO> findUserByUsernameNameEmail(String userInfo) {
        userInfo=userInfo+"%";
        String sql="select * from user where username like ? or name like ? or email like ?";

        List<UserDO> list = template.query(sql, new BeanPropertyRowMapper<>(UserDO.class),userInfo,userInfo,userInfo);
        return list;
    }

    /**
     * 查找所有的评论
     * @return
     * @author wgd
     */
    @Override
    public List<CommentsDO> selectAllComment() {
        //sql编写:
        String sql = "select * from comments";
        List<CommentsDO> list = template.query(sql, new BeanPropertyRowMapper<>(CommentsDO.class));
        return list;
    }

    /**
     * 根据id 删除评论
     * @param com_id
     */
    @Override
    public void deleteCommentById(String com_id) {
        String sql="delete from comments where com_id = ?";
        template.update(sql,com_id);
    }

    /**
     * 通过userId查询用户
     * @param userId
     * @return
     * @author wgd
     */
    @Override
    public List<CommentsDO> findCommentByUserId(String userId) {
        String sql = "select * from comments where uid = ?";
        List<CommentsDO> list = template.query(sql, new BeanPropertyRowMapper<>(CommentsDO.class),userId);
        return list;
    }

    /**
     * 通过uid查找用户邮箱
     * @param uid
     * @return
     * @author wgd
     */
    @Override
    public String findUserEmailByUid(String uid) {
        String sql = "select email from user where uid = ?";
        String email = template.queryForObject(sql, String.class, uid);
        return email;
    }

    /**
     * 通过aid查询评论
     * @param adminId
     * @return
     */
    @Override
    public List<CommentsDO> findCommentByAdminId(String adminId) {
        String sql = "select * from comments where aid = ?";
        List<CommentsDO> list = template.query(sql, new BeanPropertyRowMapper<>(CommentsDO.class),adminId);
        return list;
    }

    /**
     * 通过时间段找评论
     * @param timeQuantum
     * @return
     * @author wgd
     */
    @Override
    public List<CommentsDO> findCommentByTimeQuantum(String timeQuantum) {
        String sql = "SELECT * FROM comments WHERE DATEDIFF(create_time,NOW())<=0 AND DATEDIFF(create_time,NOW())>=-?;";
        //System.out.println("sql"+sql);
        List<CommentsDO> list = template.query(sql, new BeanPropertyRowMapper<>(CommentsDO.class),timeQuantum);
        //System.out.println("列表"+list.size());
        return list;
    }

    /**
     * 计算user的总条数
     * @return
     * @author wgd
     */
    @Override
    public long countUser() {
        String sql = "select count(*) from user";
        long count = template.queryForObject(sql, long.class);
        return count;
    }

    /**
     * 查询所有动漫简单信息
     * @param star
     * @param end
     * @author zqm
     * @return
     */
    @Override
    public List<UserDO> listUser(int star, int end) {
        String sql = "select * from user limit ?,?";
        List<UserDO> list = null;
        list = template.query(sql, new BeanPropertyRowMapper<>(UserDO.class), star, end);
        if (list == null || list.size() <= 0 ) {
            return null;
        } else {
            return list;
        }
    }

    @Override
    public List<CommentsDO> listComment(int star, int end) {
        String sql = "select * from comments limit ?,?";
        List<CommentsDO> list = null;
        list = template.query(sql, new BeanPropertyRowMapper<>(CommentsDO.class), star, end);
        if (list == null || list.size() <= 0 ) {
            return null;
        } else {
            return list;
        }
    }

    /**
     * 计算评论的总数
     * @return
     */

    @Override
    public long countComment() {
        String sql = "select count(*) from comments";
        long count = template.queryForObject(sql, long.class);
        return count;
    }

    @Override
    public List<CommentsDO> findTopCommentByLikeNum(String number) {
        String sql="select * from comments order by like_num desc limit ?";
        List<CommentsDO> commentList=template.query(sql,new BeanPropertyRowMapper<>(CommentsDO.class),Integer.parseInt(number));
        return commentList;
    }

    /**
     * 查询被禁言的用户
     * @return
     * @author wgd
     */
    @Override
    public List<UserDO> findBannedUser() {
        String sql="select * from user where banned = ?";
        List<UserDO> list=template.query(sql,new BeanPropertyRowMapper<>(UserDO.class),"Y");
        return list;
    }

    /**
     * 根据id查询用户
     * @param userInfo
     * @return
     * @author wgd
     */
    @Override
    public List<UserDO> findUserById(String userInfo) {
        String sql="select * from user where uid = ?";
        List<UserDO> list=template.query(sql,new BeanPropertyRowMapper<>(UserDO.class),userInfo);
        return list;
    }

    /**
     * 根据用户名查询用户
     * @param userInfo
     * @return
     * @author wgd
     */
    @Override
    public List<UserDO> findUserByUsername(String userInfo) {
        String sql="select * from user where username = ?";
        List<UserDO> list=template.query(sql,new BeanPropertyRowMapper<>(UserDO.class),userInfo);
        return list;
    }

    /**
     * 根据邮箱查询用户
     * @param userInfo
     * @return
     * @author wgd
     */
    @Override
    public List<UserDO> findUserByEmail(String userInfo) {
        String sql="select * from user where email = ?";
        List<UserDO> list=template.query(sql,new BeanPropertyRowMapper<>(UserDO.class),userInfo);
        return list;
    }

}
